﻿using System;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using Shoozla.ViewModel;
using Shoozla.DataModel;
using Shoozla.DataModel.Helper;
using MahApps.Metro.Controls;
using System.Windows.Controls.DataVisualization.Charting;
using System.Collections.Generic;

namespace Shoozla.View
{
    /// <summary>
    /// Interaction logic for ProgressBarView.xaml
    /// </summary>
    public partial class ResultView : MetroWindow
    {
        public ResultView() : base()
        {
            InitializeComponent();           
            DataContext = new ResultViewModel(AppModel.CoverImageCache, AppModel.NotFoundMp3,this,App.MainView);
            ((ResultViewModel)DataContext).RequestClose += (s, e) => this.Close();
            CreateCharts();
        }

        private void album_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            
            var thumb = (CoverThumb)albumLV.SelectedItem;
            ((ResultViewModel)DataContext).DisplayCover(thumb);
           
        }

        private void song_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var thumb = (CoverThumb)songLV.SelectedItem;
            ((ResultViewModel)DataContext).DisplayCover(thumb);

        }

        private void artist_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var thumb = (CoverThumb)artistLV.SelectedItem;
            ((ResultViewModel)DataContext).DisplayCover(thumb);
        }
     
        private void CreateCharts()
        {
            //pie chart, found vs not found
            Dictionary<String, int> summaryValues = new Dictionary<string, int>();
            summaryValues.Add("Artworks found", App.StatisticLogger.FileMatched);
            summaryValues.Add("Songs not found", App.StatisticLogger.TotalSearchedFiles - App.StatisticLogger.FileMatched);

            ((PieSeries)summaryChart.Series[0]).ItemsSource = summaryValues;

            //bar chart, album/artist/song
            Dictionary<String, int> splittedValues = new Dictionary<string, int>();
            splittedValues.Add("Album", App.StatisticLogger.AlbumFound);
            splittedValues.Add("Title", App.StatisticLogger.SongFound);
            splittedValues.Add("Artist", App.StatisticLogger.ArtistFound);


            ((ColumnSeries)splittedChart.Series[0]).ItemsSource = splittedValues;
        }

        private void summaryTC_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //when the user select the last tabitem i display the Chart instead of the album cover
            //if (summaryTC.SelectedIndex == summaryTC.Items.Count - 1)
            //{
            //    ((ResultViewModel)DataContext).ShowCharts = true;

            //    //pie chart, found vs not found
            //    Dictionary<String,int> summaryValues = new Dictionary<string,int>();
            //    summaryValues.Add("Artworks found", App.StatisticLogger.FoundCovers);
            //    summaryValues.Add("Songs not found", App.StatisticLogger.NotFoundCovers);

            //    ((PieSeries)summaryChart.Series[0]).ItemsSource = summaryValues;

            //    //bar chart, album/artist/song
            //    Dictionary<String, int> splittedValues = new Dictionary<string, int>();
            //    splittedValues.Add("Artist+Album", App.StatisticLogger.AlbumFound);
            //    splittedValues.Add("Artist+Song", App.StatisticLogger.SongFound);
            //    splittedValues.Add("Artist", App.StatisticLogger.ArtistFound);


            //    ((ColumnSeries)splittedChart.Series[0]).ItemsSource = splittedValues;

                
            //}
            //else
            //{
            //    var tabItem = summaryTC.Items[summaryTC.SelectedIndex];

             
            //}

            switch (summaryTC.SelectedIndex)
            {
                case 0: { album_SelectionChanged(null, null); ((ResultViewModel)DataContext).ShowCharts = false;  break; }  //refresh of the image
                case 1: { song_SelectionChanged(null, null); ((ResultViewModel)DataContext).ShowCharts = false;  break; }   //refresh of the image
                case 2: { artist_SelectionChanged(null, null); ((ResultViewModel)DataContext).ShowCharts = false;  break; } //refresh of the image
                case 3: { ((ResultViewModel)DataContext).ShowCharts = true; break; }
            }

          

        }   
    }
}
